package com.earnings.okhttputils.utils.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.UrlConstant.YUrlAddress;
import com.earnings.okhttputils.utils.bean.DetailData;
import com.earnings.okhttputils.utils.bean.DetailZData;
import com.earnings.okhttputils.utils.god.GodAdapter.GodArrayList;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/8 0008.
 */

public class DetailAdatper extends YBaseAdapter<DetailData> {

    public DetailAdatper(int layoutId) {
        super(layoutId);
    }

    private Loadmore loadmore;

    @Override
    public void onBind(YViewHolder holder, int position, DetailData data) {
        final Context context = holder.itemView.getContext();
        holder.setText(R.id.top_text_view, data.getMonth() + "月");
        RecyclerView recyclerView = holder.getView(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(context) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        GodArrayList<DetailZData> arrayList = new GodArrayList<>();
        arrayList.addAll(data.getList());
        recyclerView.setAdapter(new GodBaseAdapter<DetailZData>(R.layout.item_detail2, arrayList) {
            @Override
            public void onBind(GodViewHolder holder, int position, DetailZData data) {
                holder.setText(R.id.name_tv, data.getTitle());
                Glide.with(context).load(YUrlAddress.INDEX_URL + data.getImg()).into((ImageView) holder.getView(R.id.icon_img));
                holder.setText(R.id.detail_time_tv, data.getCreatetime());
                holder.setText(R.id.detail_number_tv, "+" + data.getRichchange());
                holder.setText(R.id.gettreasure, data.getContent());
            }
        });
        if (position == getDatas().size() - 1) {
            recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
                //用来标记是否正在向最后一个滑动
                boolean isSlidingToLast = false;

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    // 当不滚动时
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        //获取最后一个完全显示的ItemPosition
                        int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
                        int totalItemCount = manager.getItemCount();

                        // 判断是否滚动到底部，并且是向右滚动
                        if (lastVisibleItem == (totalItemCount - 1) && isSlidingToLast) {
                            //加载更多功能的代码
                            loadmore.Load();
                        }
                    }
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    //dx用来判断横向滑动方向，dy用来判断纵向滑动方向
                    if (dy > 0) {
                        //大于0表示正在向右滚动
                        isSlidingToLast = true;
                    } else {
                        //小于等于0表示停止或向左滚动
                        isSlidingToLast = false;
                    }
                }
            });
        }


    }

    public void setLoadmore(Loadmore loadmore) {
        this.loadmore = loadmore;
    }

    public interface Loadmore {
        void Load();
    }


}
